<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

class EditPage extends Think {
   	//总条数
   protected $totalRows;
   //当前是第几条记录
   
   protected $nowid;
   
   protected $now;
    // 当前记录
    protected $nowRow;
    //第一条记录
    protected $firstRow;
    //最后一条记录
    protected $lastRow;
    
    protected $preRow;
    
    protected $nextRow;
    
    protected $func;
    
    
    protected $config =array('first'=>'第一页','prev'=>'上一页','next'=>'下一页','last'=>'最后一页','theme'=>' %firstRow% %preRow% [ 第 %nowRow% 笔  / 共 %totalRows% 笔 ]  %goPage% %nextRow% %lastRow%');

    /**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param array $totalRows  总的记录数
     * @param array $listRows  每页显示记录数
     * @param array $parameter  分页跳转的参数
     +----------------------------------------------------------
     */
    public function __construct($tabname,$nowRow,$func) {
        $db = Db::getInstance();
        
        if(!empty($_SESSION['message']))
        {
        	$msg=" AND ".$_SESSION['message']." order by id";
        }
        else
        {
        	$msg=" order by id";
        }
        $sqlmin="select min(id) from ".$tabname." where 1=1 ".$msg;
        $sqlmax="select max(id) from ".$tabname." where 1=1 ".$msg;
        $sqlpre="select max(id) from ".$tabname." where id<".$nowRow.$msg;
        $sqlnext="select min(id) from ".$tabname." where id>".$nowRow.$msg;
        $sqltotal="select count(id) from ".$tabname." where 1=1 ".$msg;
        $sqlnow="select count(id) from ".$tabname." where id<".$nowRow.$msg;
        $rsmin=$db->query($sqlmin);
        $rsmax=$db->query($sqlmax);
        $rspre=$db->query($sqlpre);
        $rsnext=$db->query($sqlnext);
        $now=$db->query($sqlnow);
        $total=$db->query($sqltotal);
        $_SESSION['total']=$total;
        $this->now=$now[0]["count(id)"]+1;
        $this->nowid=$nowRow;
        $this->totalRows=$total[0]["count(id)"];
        $this->firstRow =$rsmin[0]["min(id)"];
        $this->lastRow=$rsmax[0]["max(id)"];
        $this->preRow=$rspre[0]["max(id)"];
        $this->nextRow=$rsnext[0]["min(id)"];
        $this->func=$func;
        if(!empty($func))
        {
        	$this->func=$func;
        }
        else
        {
        	$this->func="edit";
        }
        
    }

    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }

    /**
     +----------------------------------------------------------
     * 分页显示输出
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    public function show() {
    	
        if($this->firstRow==$this->nowid)
        {
        	$firstStr="<input type='button' value='' title='第一笔' style='background:url(__PUBLIC__/Images/resultset_first01.gif);width:16px;height:16px;border:0;cursor:hand'/>";
        }
        else
        {
        	 $firstStr="<input type='button' value='' title='第一笔' onclick='edits($this->firstRow,\"$this->func\")' style='background:url(__PUBLIC__/Images/resultset_first.png);width:16px;height:16px;border:0;cursor:hand'/>";
        }
       	if($this->lastRow ==$this->nowid)
       	{
       		 $lastStr="<input type='button' value='' title='最后一笔' style='background:url(__PUBLIC__/Images/resultset_last01.gif);width:16px;height:16px;border:0;cursor:hand'/>";
       	}
       	else
       	{
       		 $lastStr="<input type='button' value='' title='最后一笔' onclick='edits($this->lastRow,\"$this->func\")' style='background:url(__PUBLIC__/Images/resultset_last.png);width:16px;height:16px;border:0;cursor:hand'/>";
       	}
 
        if($this->nowid==$this->firstRow)
        {
        	$prevRowStr="<input type='button' value='' title='上一笔' style='background:url(__PUBLIC__/Images/resultset_previous01.gif);width:16px;height:16px;border:0;cursor:hand'/>";
        }
        else
        {	
            $prevRowStr="<input type='button' value='' title='上一笔' onclick='edits($this->preRow,\"$this->func\")' style='background:url(__PUBLIC__/Images/resultset_previous.png);width:16px;height:16px;border:0;cursor:hand'/>";
        }

        if($this->nowid==$this->lastRow)
        {
        	$nextRowStr="<input type='button' value='' title='下一笔' style='background:url(__PUBLIC__/Images/resultset_next01.gif);width:16px;height:16px;border:0;cursor:hand'/>";
        }
        else
        {
            $nextRowStr="<input type='button' value='' title='下一笔' onclick='edits($this->nextRow,\"$this->func\")' style='background:url(__PUBLIC__/Images/resultset_next.png);width:16px;height:16px;border:0;cursor:hand'/>";
        }
        //$gopage="<table border='solid 1px'><tr><td style='border-bottom-color:red'><input id='pagenum' align='middle' type='text' style='width:30px;height:10px'> <input type='button' align='middle' style='border:0;height:16px;width:24px;cursor:hand;background:url(\"__PUBLIC__/Images/goPage.gif\")' onclick='edits(-100,\"$this->func\")'></td></tr></table>";
        $gopage="<input id='pagenum' align='middle' type='text' style='width:30px;height:15px'> <input type='button' align='middle' style='border:0;height:16px;width:24px;cursor:hand;background:url(\"__PUBLIC__/Images/goPage.gif\")' onclick='edits(-100,\"$this->func\")'>";
        
        //组装输出字符串
        $pageStr= str_replace(
            array( '%firstRow%','%preRow%','%nowRow%','%totalRows%','%nextRow%','%lastRow%','%goPage%'),
            array($firstStr,$prevRowStr,$this->now,$this->totalRows,$nextRowStr,$lastStr,$gopage),$this->config['theme']);
           
        return $pageStr;
    }

}
?>